﻿namespace Rzrlf.SP.Innovator.Bdc.Utils
{
    internal class IomUtil
    {
        internal static Aras.IOM.Item GetItems(string itemType)
        {
            Aras.IOM.Innovator invtr = GetIomInstance();

            Aras.IOM.Item item = invtr.newItem(itemType, "get");

            Aras.IOM.Item result = item.apply();

            return result;
        }

        #region Helper Methods...

        private static Aras.IOM.Innovator GetIomInstance()
        {
            Aras.IOM.Innovator invtr = Aras.IOM.IomFactory.CreateInnovator(null);

            //Aras.IOM.WinAuthHttpServerConnection con = Aras.IOM.IomFactory.CreateWinAuthHttpServerConnection(url, database);
            Aras.IOM.HttpServerConnection con = Aras.IOM.IomFactory.CreateHttpServerConnection(Rzrlf.SP.Innovator.Bdc.Settings.InnovatorServerUrl, Rzrlf.SP.Innovator.Bdc.Settings.InnovatorDatabaseName, Rzrlf.SP.Innovator.Bdc.Settings.InnovatorUserName, Rzrlf.SP.Innovator.Bdc.Settings.InnovatorPassword);

            Aras.IOM.Item result = con.Login();
            if (result.isError())
            {
                throw new System.Security.SecurityException(string.Format("The requested login failed for user {0}.", Rzrlf.SP.Innovator.Bdc.Settings.InnovatorUserName));
            }

            invtr = Aras.IOM.IomFactory.CreateInnovator(con);

            return invtr;
        }

        #endregion
    }
}
